package com.goldsprite.gameframeworks.ecs.system;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class RenderLayerManager {
	private static final List<String> spriteLayers = new ArrayList<>();
	private static final Map<String, Integer> layerDepths = new ConcurrentHashMap<>();
	private static final Map<String, RenderLayerConfig> layerConfigs = new ConcurrentHashMap<>();
	private static boolean needsSort = false;


	public static void createDefaultGameLayers() {
		// 预定义常用渲染层
		// 游戏特定的渲染层
		createLayer("Default", 000, true);
		createLayer("FarBackground0", 85, true);
		createLayer("FarBackground", 90, true);
		createLayer("MiddleBackground", 95, true);
		createLayer("Background", 100, true);
		createLayer("Block", 200, true);
		createLayer("Entity", 300, true);
		createLayer("Player", 301, true);
		createLayer("Item", 400, true);
		createLayer("Foreground", 500, true);
		createLayer("WorldGUI", 600, true);
		createLayer("Effect", 700, true);
		createLayer("WorldUI", 800, true);
		createLayer("UI", 800, false);
		createLayer("Overlay", 1000, false);
		createLayer("Debug", 1100, true);
	}
	static {
	}

	public static class RenderLayerConfig {
		public String name;
		public int depth;
		public boolean useWorldSpace;
		public boolean enabled = true;

		public RenderLayerConfig(String name, int depth, boolean useWorldSpace) {
			this.name = name;
			this.depth = depth;
			this.useWorldSpace = useWorldSpace;
		}
	}

	public static synchronized void createLayer(String layerName, int depth, boolean useWorldSpace) {
		if (!layerDepths.containsKey(layerName)) {
			spriteLayers.add(layerName);
			layerDepths.put(layerName, depth);
			layerConfigs.put(layerName, new RenderLayerConfig(layerName, depth, useWorldSpace));
			needsSort = true;
			sortLayersIfNeeded();
		}
	}

	public static synchronized void deleteLayer(String layerName) {
		spriteLayers.remove(layerName);
		layerDepths.remove(layerName);
		layerConfigs.remove(layerName);
	}

	public static List<String> getSortedLayers() {
		sortLayersIfNeeded();
		return new ArrayList<>(spriteLayers);
	}

	public static int getLayerDepth(String layerName) {
		return layerDepths.getOrDefault(layerName, 0);
	}

	public static boolean isWorldSpaceLayer(String layerName) {
		RenderLayerConfig config = layerConfigs.get(layerName);
		return config != null && config.useWorldSpace;
	}

	public static boolean isLayerEnabled(String layerName) {
		RenderLayerConfig config = layerConfigs.get(layerName);
		return config != null && config.enabled;
	}

	public static void setLayerEnabled(String layerName, boolean enabled) {
		RenderLayerConfig config = layerConfigs.get(layerName);
		if (config != null) {
			config.enabled = enabled;
		}
	}

	public static RenderLayerConfig getLayerConfig(String layerName) {
		return layerConfigs.get(layerName);
	}

	public static synchronized void setLayerDepth(String layerName, int newDepth) {
		if (layerDepths.containsKey(layerName)) {
			layerDepths.put(layerName, newDepth);
			RenderLayerConfig config = layerConfigs.get(layerName);
			if (config != null) {
				config.depth = newDepth;
			}
			needsSort = true;
		}
	}

	private static synchronized void sortLayersIfNeeded() {
		if (needsSort) {
			spriteLayers.sort((a, b) -> {
				int depthA = layerDepths.getOrDefault(a, 0);
				int depthB = layerDepths.getOrDefault(b, 0);
				return Integer.compare(depthA, depthB);
			});
			needsSort = false;
		}
	}

	public static void debugLayers() {
		System.out.println("=== RenderLayerManager Debug Info ===");
		List<String> sorted = getSortedLayers();
		for (String layer : sorted) {
			RenderLayerConfig config = getLayerConfig(layer);
			System.out.println(String.format("Layer: %-15s Depth: %-5d WorldSpace: %-6b Enabled: %-6b",
				layer, config.depth, config.useWorldSpace, config.enabled));
		}
	}
}
